#include "view_time_3.h"
#include "view_time_3.inc"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "view_times.h"
#if defined(TIME_3_SKIP)
#else
void show_time_3_percent(uint8_t const *base, uint8_t const *full, int percent, int x, int y, int inner_diameter)
{
    if (percent < 0)
    {
        percent = 0;
    }
    else if (percent > 100)
    {
        percent = 100;
    }
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, base);
    disp_draw_user_image_percent_circle(x, y, full, percent, inner_diameter);
}
void view_time_3_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_get_level(battery_ctrl_get_percent());
        disp_ctrl.com_data.i_temp[2] = UIdatetime.date.month;
        disp_ctrl.com_data.i_temp[3] = UIdatetime.date.day;
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.hour;
        disp_ctrl.com_data.i_temp[5] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.second;
        disp_ctrl.com_data.i_temp[7] = if_time_hour_system_12();
        if (disp_ctrl.com_data.i_temp[7])
        {
            disp_ctrl.com_data.i_temp[7] += (disp_ctrl.com_data.i_temp[4] >= 12); // 1=am,2=pm
            disp_ctrl.com_data.i_temp[4] %= 12;
            if (disp_ctrl.com_data.i_temp[4] == 0)
            {
                disp_ctrl.com_data.i_temp[4] = 12;
            }
        }

        disp_ctrl.com_data.i_temp[9] = get_present_week();
        disp_ctrl.com_data.i_temp[10] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[11] = hr_get_hr_data();
        int max_heart_rate = 220 - system_config_get_age();
        int min_heart_rate = 40;
        disp_ctrl.com_data.i_temp[12] = (disp_ctrl.com_data.i_temp[11] - min_heart_rate) * 100 / (max_heart_rate - min_heart_rate);
        disp_ctrl.com_data.i_temp[13] = sport_get_calorie();
        disp_ctrl.com_data.i_temp[14] = disp_ctrl.com_data.i_temp[13] * 100 / system_config_get_target_calorie();
        disp_ctrl.com_data.i_temp[15] = sport_get_present_step();
        disp_ctrl.com_data.i_temp[16] = disp_ctrl.com_data.i_temp[15] * 100 / system_config_get_target_step();
        // #warning debug
        //         disp_ctrl.com_data.i_temp[12] = disp_ctrl.auto_refresh_count % 101;
        //         disp_ctrl.com_data.i_temp[16] = disp_ctrl.auto_refresh_count % 101;
        if (disp_ctrl.com_data.i_temp[12] < 0)
        {
            disp_ctrl.com_data.i_temp[12] = 0;
        }
        else if (disp_ctrl.com_data.i_temp[12] > 100)
        {
            disp_ctrl.com_data.i_temp[12] = 100;
        }
        if (disp_ctrl.com_data.i_temp[14] < 0)
        {
            disp_ctrl.com_data.i_temp[14] = 0;
        }
        else if (disp_ctrl.com_data.i_temp[14] > 100)
        {
            disp_ctrl.com_data.i_temp[14] = 100;
        }
        if (disp_ctrl.com_data.i_temp[16] < 0)
        {
            disp_ctrl.com_data.i_temp[16] = 0;
        }
        else if (disp_ctrl.com_data.i_temp[16] > 100)
        {
            disp_ctrl.com_data.i_temp[16] = 100;
        }
        disp_ctrl.auto_refresh_time = 1000;
    }
    int x = 0;
    int y = 0;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_4113_background_0_0);
    x = 83;
    y = 10;
    if (disp_ctrl.com_data.i_temp[0])
    {

        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }
    x = 133;
    y = 12;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_battery_back);
    if (disp_ctrl.com_data.i_temp[10])
    {
        disp_draw_user_image_ignore_color(x + 9, y + 0, RGB565_COLOR_BLACK, bmp_battery_charging);
    }
    else
    { // 2 2 7 2 12 2
        // 4x8 0x9cd3
        uint16_t color = 0x9cd3;
        if (disp_ctrl.com_data.i_temp[1] == 1)
        {
            color = 0x9020;
        }
        x += 2;
        y += 2;
        for (int i = 0; i < disp_ctrl.com_data.i_temp[1]; i++)
        {
            disp_draw_rect_color(x, y, x + 4 - 1, y + 8 - 1, color);
            x += 5;
        }
    }
    x = 107;
    y = 154;
    disp_draw_rect_color(x, y, x + 7, y + 7, RGB565_COLOR_WHITE);
    y = 132;
    disp_draw_rect_color(x, y, x + 7, y + 7, RGB565_COLOR_WHITE);
    x = 93;
    y = 181;
    disp_draw_user_image(x, y, time_1_slash_82_168);
    x = 77;
    y = 183;
    disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[2], 2, 8, num_6x14_TAB);
    x = 101;
    disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[3], 2, 8, num_6x14_TAB);

    x = 136;
    y = 183;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_2010_week_mon_89_139 + disp_ctrl.com_data.i_temp[9] - 1);

    x = 194;
    y = 128;
    if (disp_ctrl.com_data.i_temp[7] == 1)
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_4211_am_194_128);
        disp_draw_user_image(x, y, bmp_time_3_am);
    }
    else if (disp_ctrl.com_data.i_temp[7] == 2)
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_4212_pm_194_128);
        disp_draw_user_image(x, y, bmp_time_3_pm);
    }
    x = 194;
    y = 153;
    disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[6], 2, 8, num_time_2_7x17_TAB);
    x = 40;
    y = 120;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_4200_time_0 + disp_ctrl.com_data.i_temp[4] / 10);
    x = 72;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_4200_time_0 + disp_ctrl.com_data.i_temp[4] % 10);
    x = 129;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_4200_time_0 + disp_ctrl.com_data.i_temp[5] / 10);
    x = 160;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_4200_time_0 + disp_ctrl.com_data.i_temp[5] % 10);

    // #warning debug
    //     disp_ctrl.com_data.i_temp[14] = 52;
    //     disp_ctrl.com_data.i_temp[16] = 51;
    //     disp_ctrl.com_data.i_temp[12] = 0;
    show_time_3_percent(time_3_calorie_base, time_3_calorie_full, disp_ctrl.com_data.i_temp[14], 28, 45, 36);
    show_time_3_percent(time_3_step_base, time_3_step_full, disp_ctrl.com_data.i_temp[16], 92, 45, 38);

    user_cycle_percent_param_t const param = {
        .base = bmp_heart_rate_base,
        .transparent_color = RGB565_COLOR_BLACK,
        .full_color = 0xeb0a,
        .empty_color = 0x38a2,
        .x = 152,
        .y = 36,
        .target_num = 100,                              // 1000
        .presently_num = disp_ctrl.com_data.i_temp[12], // 2600
        .temp_0 = &disp_ctrl.com_data.i_temp[17],
        .temp_1 = &disp_ctrl.com_data.i_temp[18],
        .temp_2 = &disp_ctrl.com_data.f_temp[19],
        .temp_3 = &disp_ctrl.com_data.i_temp[20],
        .temp_4 = &disp_ctrl.com_data.i_temp[21],
        .line_width = 3,
    };
    disp_draw_user_image_cycle_percent(&param);
    x = 43;
    y = 58;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_calories_icon);
    x = 106;
    y = 58;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_steps_icon);
    x = 177;
    y = 86;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_heart_rate_icon);
    // x = 176;
    // y = 67;
    // disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_small_bpm_unit);
    y = 56;
    x = 152 + 31;
    disp_draw_user_image_num_mid_ignore(x, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[11], 8, num_time_2_7x17_TAB);
}
#endif
